import type {HttpRequestConfig, HttpTask} from "luch-request";
import type {RequestMonitor} from "@/request/util/RequestMonitor";

export const S4 = () => {
    return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
}

export let getUniCode=()=>{
    return `${S4()}-${S4()}-${S4()}`;
}

/**
 * 请求开始，调用rm的start方法传入生成的unicode
 * start创建定时器并和改unicode绑定,（3秒后存到阻塞数组，并返回定时器的timer）
 * @param rm
 * @param config
 */
export let startRequest=(rm:RequestMonitor,config: HttpRequestConfig<HttpTask>)=>{
    let code=getUniCode()
    config.custom={code}
    rm.start(code)
}

/**
 * 响应以后跟根据unicode清除本地请求对应的定时器
 * @param rm
 * @param config
 */
export let finishRequest=(rm:RequestMonitor,config: HttpRequestConfig<HttpTask>)=> {
    //@ts-ignore
    rm.finish(config.custom.code)
}